package sip

import (
	"time"

	//"github.com/penggy/EasyGoLib/redis"
	"gitee.com/larry_wan/go-lib/utils"
)

type Stream struct {
	StreamID      string
	DeviceID      string
	SenderID      string
	SenderName    string
	SenderSeq     string // Subject: SenderID:SenderSeq,ReceiverID:ReceiverSeq
	MSDeviceID    string
	SSRC          string // [0|1] + SenderID[3:8] + SenderID[16:20]
	Transport     string // UDP/TCP
	TransportMode string //passive active
	UseForSnap    bool

	PlaybackStartTime    string //YYYY-MM-DDTHH:mm:ss
	PlaybackEndTime      string //YYYY-MM-DDTHH:mm:ss
	PlaybackFileURL      string
	PlaybackFileSize     uint64
	PlaybackFileHTTPSURL string
	PlaybackCMD          string

	DevSessionID string
	MSSessionID  string

	StreamUrlIP string

	StartAt   string //YYYY-MM-DD HH:mm:ss
	FLV       string
	HTTPS_FLV string
	RTMP      string
	RTSP      string
	HLS       string
	HTTPS_HLS string
	WS_FLV    string
	WSS_FLV   string
	CDN       string

	RTPInBytes      int64
	RTPTimestampSec int64

	InBytes      int64
	OutBytes     int64
	NumOutputs   int64
	InBitrate    int64
	OutBitrate   int64
	AudioBitrate int64
	VideoBitrate int64
	VideoHeight  int64
	VideoWidth   int64
	VideoFPS     int64

	Cascade            string
	CCTransport        string // UDP/TCP
	Record             uint
	MediaInfoType      string
	MediaInfoPort      int
	MediaInfoProto     string
	MediaInfoMode      string
	VideoCode          string
	LatestGetFrameTime string
}
/*
func (stream *Stream) WriteToRedis(d time.Duration) (err error) {
	err = redis.HSetStruct(stream.StreamID, stream, d)
	return
}
*/

// seconds from start time
func (stream *Stream) Duration() (secs int64) {
	startAt, err := time.ParseInLocation(utils.DateTimeLayout, stream.StartAt, time.Local)
	if err != nil {
		return
	}
	duration := time.Now().Sub(startAt)
	secs = int64(duration.Seconds())
	return
}

// seconds of playback duration
func (stream *Stream) PlaybackDuration() (secs int64) {
	start, err := time.ParseInLocation(DATE_FORMAT, stream.PlaybackStartTime, time.Local)
	if err != nil {
		return
	}
	end, err := time.ParseInLocation(DATE_FORMAT, stream.PlaybackEndTime, time.Local)
	if err != nil {
		return
	}
	secs = int64(end.Sub(start).Seconds())
	if secs < 0 {
		secs = 0 - secs
	}
	return
}

func (stream *Stream) Progress() float32 {
	d := stream.PlaybackDuration()
	if d <= 0 {
		return 0
	}
	t := stream.RTPTimestampSec
	if t < 0 {
		t = 0 - t
	}
	return float32(t) / float32(d)
}
